#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

from terms import *
import re
from math import ceil
from record import *
from urlcheck import checkUrl
from util import *
from oaicookie import *
from history import *

def singleEscape(terms):
	quoteSwap = re.compile('\'')
	return quoteSwap.sub('&apos;',terms)

# class for outputing the page
class output:
	def __init__(self, total, loc, max, terms, termlist):
		self.total_results = total
		self.max_result = max
		self.terms = terms
		self.loc = loc
		self.termlist = termlist
		# result number
		self.resnum = 0
		# read in variables

	def print_result_bar(self,result_end,duration):
		# no results?
		if result_end == 0:
			self.loc = 0
		
		# print results bar
		data = (shorten(self.terms,40), self.loc, result_end, self.total_results, duration)
		outbit('pg_bar_text', data)

		# print the search box
		data = (singleEscape(self.terms),singleEscape(self.terms))
		outbit('pg_search_results', data)

	# print a record
	def print_rec(self,results, expanded = 0):
		# record headers
		print "<div class=\"margin1em\">"
		print "<div class=\"resultShad\">"
		validUrl = checkUrl(results,1)
		if validUrl == '':
			print "<div id=\"res%s\" class=\"resultItem badurl\">" % self.resnum
		else:
			print "<div id=\"res%s\" class=\"resultItem\">" % self.resnum

		if expanded == '1':
			# print expanded output
			print_rec_expand(results,self.terms,self.resnum)
		else:
			# print a shortened record output
			print_rec_short(results,self.terms,self.resnum)

		# print footers
		print "</div></div>"
		print "</div>"

		# increment result number
		self.resnum += 1

	def header(self):
		outbit('pg_header_3')

	def searchSubHeader(self):
		data = (showHistory(1),printTabs(1))
		outbit('pg_search_sub',data)

	def search_bar(self):
		outbit('pg_title_2')

	def footer(self,within):
		# max links on either side of current page
		maxLinks = 8

		pages = ceil(min(1000, self.total_results) / (self.max_result * 1.0))
		if (pages - 1) * self.max_result< self.total_results:
			pages += 1

		if int(pages) > 1:
			print """<br><div class=\"pages\">Page: &nbsp;&nbsp;"""

		for p in range(0, int(pages) - 1):
			if self.loc >= p*self.max_result +1 and self.loc < (p+1)*self.max_result:
				print p+1
			elif self.loc >= (p-maxLinks)*self.max_result +1 and self.loc < (p+1+maxLinks)*self.max_result:
				print "<a href=\"search.py?l=%s&s=%s&w=%s\">%s</a>" % (p*self.max_result+1, urls(self.terms), within, p+1)

		print """
		</div>
		"""

		outbit('pg_footer_3')

#eof

